var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');

var app = getApp();
Page({
  data: {
    id: 0,
    isAuth:0,
    userInfo: {
      nickName: '点击登录',
      avatarUrl: '/static/images/my.png'
    },
    userApproveInfo:{
      contactName:'',
      contactTel:'',
      contactAddress:'',
      userType:0,
      companyName:'',
      companyLogo:'',
      adminUsername:'',
      herbInfoList:[],
    },
    userTypes:[
      {value: '0', name: '个人经营户认证' , checked:true},
      {value: '1', name: '药材厂家'},
      {value: '2', name: '药材经销商'},
    ],
    logoUrls: [],
    files: [],
    qualificationUrls: [],
    qualificationFiles: [],
    herbInfoStr:''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成
    var that = this;
    that.setData({
      id: options.id,
      isAuth:options.isAuth
    }); 
    
    this.getQualificationsDetail();
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
    //获取用户的登录信息
    if (app.globalData.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');
      this.setData({
        userInfo: userInfo
      });
    }
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  previewImage: function(e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  qualificationsPreviewImage: function(e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.qualificationFiles // 需要预览的图片http链接列表
    })
  },
  getQualificationsDetail(){
    wx.showLoading({
      title: '加载中',
    });
    setTimeout(function() {
      wx.hideLoading()
    }, 2000);
    
    let that = this;
    util.request(api.QualificationsDetail, {
      authId: that.data.id,
      isAuth:that.data.isAuth
    }).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          userApproveInfo: res.data.userApproveInfo
        });
        let userType = res.data.userApproveInfo.userType;
        if(userType == '0'){
          let herbInfoList = that.data.userApproveInfo.herbInfoList;
          if(herbInfoList && herbInfoList[0]!=null){
            that.setData({
              herbInfoStr: herbInfoList.map((t)=> t.herbName).join('、'),
            });
          }
        }else{
          let companyLogoStr = that.data.userApproveInfo.companyLogo;
          if(companyLogoStr!=null && companyLogoStr!=''){
            that.setData({
              files: companyLogoStr.split(',')
            });
          }
        }
        let certificatesImageStr = that.data.userApproveInfo.certificatesImage;
        if(certificatesImageStr!=null && certificatesImageStr!=''){
          that.setData({
            qualificationFiles: certificatesImageStr.split(',')
          });
        }
      }
      wx.hideLoading();
    });
  },
})